package appbase.kit.weixin;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;

import appbase.kit.encrypt.MD5Kit;

public class WxPubPayAESKit {

	/** 
     * 密钥算法 
     */  
    private static final String ALGORITHM = "AES";  
    /** 
     * 加解密算法/工作模式/填充方式 
     */  
    private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";  
    /** 
     * 生成key 
     */  
    //private static SecretKeySpec key = new SecretKeySpec(MD5.encrptMD5("IulMark1024AtHNhyx10086QWilHl9Or", "UTF-8").toLowerCase().getBytes(), ALGORITHM);  
      
    /** 
     * AES加密 
     */  
    public static String encryptData(String text, String key) 
    {  
        try {
        	// 创建密码器  
            Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");  
            // 初始化
            SecretKeySpec secretKey = generateKey(key);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
            return Base64.toBase64String(cipher.doFinal(text.getBytes()));
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }  
  
    /** 
     * AES解密 
     */  
    public static String decryptData(String encryptText, String key)
    {
    	try {
    		SecretKeySpec secretKey = generateKey(key);
    		
    		Security.addProvider(new BouncyCastleProvider());
        	
            Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING, "BC");  
            cipher.init(Cipher.DECRYPT_MODE, secretKey);  
            return new String(cipher.doFinal(Base64.decode(encryptText)));
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    }  
  
    private static SecretKeySpec generateKey(String keyStr)
    {
    	SecretKeySpec key = new SecretKeySpec(MD5Kit.encrptMD5(keyStr, "UTF-8").toLowerCase().getBytes(), ALGORITHM);
    	return key;
    }
    
    public static void main(String[] args) throws Exception 
    {  
        String text = "aCqBrG72SA2sZZAFWC28Wd6uBsofE9OqNdpklIyWJhsK6vxEr468nveFSGmcD6wMB/wxPKdbOcdbi69UVCX5CaESMQovionO7fApWtZ1ZUwJaX6Jn6TJW23EmgE3ZlopWtc4RwTOwBfV1muESx7fPtC8XV3kp7amA5ZMLqY/xsHFBIS7BLiaaLl9kPbxgLFA5E7tSDLgupRvivcu5xLrKnYicGGbjJupxeV8VQ/FYASNInivT4hvWka1dCCZxyuouQOPnwCmE0Tb89seNdydQwZV+mQOmYqAu7m7dR+pkunRa+tMXJ+rlZqfCKbyjbJbS6rJep4NwIc5oRn8QmcyHpEQbpCZFAj8IZjskoPsdbs7FpczLrMEN7jUhgPBkyrFGHj1JCPg29EOV/IYu0psDiEzpJLzsKyJDodFCmFmBHFE7DzzMIwUOwK69XUNNJUPw6SFRChytngikQMqWNyLswd3uwY7wVy38i10q+VNehwRw4B3RPMNm83XIrAgQCZZ9hiXKyPgzKQmfetFJ8NSMCZK3iUs4p02NfqgMWSigjbg/bxzMbX48qb7tsFw0R2yLk9yH6YzWZxwY+Yq/K0Njq/WX66xbubt4ETYahjgwxON+zT9DliKg3PWN5zKpnuIknvbMNw+4dLNwdvaxWJfKkcJ2db7pyiVbj60himXKadMmHirSSJE+JKMeeDZd1UdXqV1O4f4WPPonIlxbJDQoalhc/KqwciRxDPrIqnSLE1Ec1IXD2P6oGPxqt/mrWBBg62JFqJKi9XAdzmOypymxUPzpmDvf7rtccXnk35Rbb5aoxTgfPrBpt79l1UchuArqSZSMM2p2c5JgLJYBiwOtr+Xv4v1OYUBTHrU7n7iJ+41nLZjabQzG4xpHjgZ8V6VEAMro9JFe6kA5R8Rd15vVgyVb9+ljls3Z1pd6SWUzkjKnAskA5820Uci578oFNdMZHBGuz0hGESIhx+LvVpXouGwJst+nFF0t6QTZRQOVhvjPVca2zECAHGKwsbft307BOyHj0v5XCSAEarbo+GEktN9H9aanCwaFWjUKgx9z2VCH70NHN5XthSyDMB+Utyv";
    	String key = "IulMark1024AtHNhyx10086QWilHl9Or";
        System.out.println(decryptData(text, key));  
    }  
	
	
	
}
